package com.softeem.hbsm_video.controller;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.softeem.hbsm_video.entity.Admin;
import com.softeem.hbsm_video.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RequestMapping("/admin")
//管理员控制接口
@RestController
public class AdminController {
    @Autowired
    private AdminService adminService;

    @GetMapping("/add")
    public boolean add(Admin admin) {
        //根据名称查询管理员
        boolean b = adminService.exists(Wrappers.query(Admin.class).eq("username", admin.getUsername()));
        if (b) {
            //用户已存在
            return false;
        }
        //实现对密码的加密
        //生成随机盐（token)
        String salt = RandomUtil.randomString(16);
        //将输入密码和盐组合
        String pwd = admin.getPassword() + salt;
        //使用md5算法对组合后的密码加密
        pwd = SecureUtil.md5(pwd);
        //将加密后的密码和随机盐设置到对象中
        admin.setPassword(pwd);
        admin.setSalt(salt);
        return adminService.save(admin);
    }

    @GetMapping("/login")
    public boolean login(String username, String password) {
        Admin admin = adminService.getOne(Wrappers.query(Admin.class).eq("username", username));
        return true;
    }


}
